<?php
//pre('__pre_open_email_log__', ['receiveEmail' => 'wuliqiang_aa@163.com']);
/**
 * 打印指定内容
 *
 * @Author Charles.Wu
 * @DateTime 2020-05-27
 * @param void $content   要打印的内容
 * @param void $status_type     状态，  是打印中继续执行，  还是exit 停止   1开启， 11,停止 2,开启 22， 停止;
 * @param void $path_type   是否开启打印路径模式，  true  开启，  false  停止
 * @param void $pre_code_wlq_stop_type  如果打印的内容有多个， 可以选择 在多个打印中的某个点停止， 可以选择在打印中的某个数字 开启显示 $path  第二个是all，则全部显示$path   [第几个$status=11，第几个显示$path]
 * @return void
 */
function pre($content = 'This is Empty Content', $status_type = 'Not', $path_type = 'Not', $pre_code_wlq_stop_type = 'Not')
{
    /*
        //使用方法
        pre(); //相当于 exit ， 会显示路径信息
        pre($content); //打印内容，并且显示路径信息
        pre($content, 1|2|11|22); //打印内容， 按照传入状态执行相应顺序
        pre($content, 1|2|11|22, true|false,); // 打印内容， 按照传入状态执行顺序， 根据传入Bool 判断是否显示路径
        pre($content, 1|2|11|22, true|false, [1-n, 1-n|all]); // 打印内容， 按照传入状态执行顺序， 根据传入Bool 判断是否显示路径， [第几个$status=11，第几个显示$path]
        pre('trues'); //相当于 exit ， 会显示路径信息
        pre($content, 'true'|'trues'); // 打印内容，  ture 是开启路径 $status = 11
        pre($content, false, [1-n, 1-n|all]); // 打印内容，  ture 是开启路径 $status = 11
        //内存与时间模式
        pre('__pre_show_all_start__');
        //只显示路径内存信息
        pre('__show_all__');
        //开启路径打印
        pre('__pre_open_path__'); //开启下一个
        pre('__pre_open_path_all__'); //开启所有
        //默认打印类型
        pre('__pre_default_type__', 1 || 11 || 2 || 22);
        //是否开启LOG
        pre('__pre_open_log__');
    */
    @session_start();
    $status = $status_type === 'Not' ? 11 : $status_type;
    $path = $path_type === 'Not' ? false : $path_type;
    $pre_code_wlq_stop = $pre_code_wlq_stop_type === 'Not' ? [0, 0] : $pre_code_wlq_stop_type;
    //开启内存与时间计算
    if ($content === '__pre_show_all_start__') {
        $_SESSION['__pre_start_memory__'] = $__pre_start_memory__ = memory_get_usage();
        $_SESSION['__pre_start_time__'] = $__pre_start_time__ = microtime(true);
        define('__pre_start_memory__', $__pre_start_memory__);
        define('__pre_start_time__', $__pre_start_time__);
        return false;
    }
    //开启路径显示
    if ($content === '__pre_open_path__' || $content === '__pre_open_path_all__') {
        $_SESSION['__pre_open_path__'] = $__pre_open_path__ = 'start';
        define('__pre_open_path__', $__pre_open_path__);
        //是否需要一直开启
        if ($content === '__pre_open_path_all__') {
            $_SESSION['__pre_open_path_status__'] = $__pre_open_path_status__ = 'all';
            define('__pre_open_path_status__', $__pre_open_path_status__);
        }
        return false;
    }
    //默认执行类型
    if ($content === '__pre_default_type__') {
        $_SESSION['__pre_default_type__'] = $__pre_default_type__ = $status;
        define('__pre_default_type__', $__pre_default_type__);
        if ($path === true) {
            $_SESSION['__pre_default_type_force__'] = $__pre_default_type_force__ = $path;
            define('__pre_default_type_force__', $__pre_default_type_force__);
        }
        return false;
    }
    //强制开启LOG
    if ($content === '__pre_open_log__') {
        $_SESSION['__pre_open_log__'] = $__pre_open_log__ = true;
        define('__pre_open_log__', $__pre_open_log__);
        return false;
    }
    //强制开启发送邮件
    if ($content === '__pre_open_email_log__') {
        $_SESSION['__pre_open_email_log__'] = $__pre_open_email_log__ = true;
        //存储邮件
        if (isset($status_type['receiveEmail']) && $status_type['receiveEmail']) $_SESSION['__pre_open_email_log__email_address']['receiveEmail'] = $status_type['receiveEmail'];
        if (isset($status_type['title']) && $status_type['title'])$_SESSION['__pre_open_email_log__email_address']['title'] = $status_type['title'];
        if (isset($status_type['content']) && $status_type['content'])$_SESSION['__pre_open_email_log__email_address']['content'] = $status_type['content'];
        define('__pre_open_email_log__', $__pre_open_email_log__);
        return false;
    }
    if (isset($_SESSION['__pre_open_log__']) && $_SESSION['__pre_open_log__'] === true) {
        $__pre_open_log__ = true;
    }
    if (isset($_SESSION['__pre_open_email_log__']) && $_SESSION['__pre_open_email_log__'] === true) {
        $__pre_open_email_log__ = true;
    }
    //如果只是本次使用，则删除 pre_open_path
    if (!empty($_SESSION['__pre_open_path__']) || !empty($_SESSION['__pre_open_path_status__'])) {
        if (empty($_SESSION['__pre_open_path_status__'])) unset($_SESSION['__pre_open_path__']);
        $path = true;
    }
    //查找是否开启默认打印类型
    if ((isset($_SESSION['__pre_default_type__']) && !empty($_SESSION['__pre_default_type__'])) && ($status_type === 'Not' || (isset($_SESSION['__pre_default_type_force__']) && $_SESSION['__pre_default_type_force__'] === true))) {
        $status = $_SESSION['__pre_default_type__'];
        unset($_SESSION['__pre_default_type__']);
        unset($_SESSION['__pre_default_type_force__']);
    }
    $backtrace = debug_backtrace();
    if (is_array($content) && $status === 11 && $path === false && $pre_code_wlq_stop === [0, 0]) {
        $content_array = $content;
        foreach ($content_array as $k => $v) {
            $$k = $v;
        }
    }
    $pre_log_all_content = '';
    $_SESSION['__pre_code_wlq__'] = empty($_SESSION['__pre_code_wlq__']) ? 1 : $_SESSION['__pre_code_wlq__'] + 1;
    $pre_code_wlq = (int) $_SESSION['__pre_code_wlq__'];
    $pre_log_all_content .= "\r\n\r\n<xmp> _____[{$pre_code_wlq}]【   Start Pre   】[{$pre_code_wlq}] _____ \r\n";
    $end_pre = "\r\n\r\n _____[{$pre_code_wlq}]【   End   Pre   】[{$pre_code_wlq}] _____  </xmp>\r\n";
    $path_array = ($status === false && is_array($path) && ($path[1] > 0 && $pre_code_wlq === $path[1]));
    if ($content === 'This is Empty Content' || (isset($path[1]) && $path[1] === 'all') || (isset($pre_code_wlq_stop[1]) && $pre_code_wlq_stop[1] === 'all')) {
        $path = true;
    }
    if ($content === 'show_all' || $content === 'show_all_not_end' || $path === true || in_array($status, ['true', 'trues']) || (isset($pre_code_wlq_stop[1]) && $pre_code_wlq_stop[1] > 0 && $pre_code_wlq === $pre_code_wlq_stop[1]) || $path_array) {
        $pre_log_all_content .= "\r\n [ Using the path ]     \r\n\r\n";
        $args = '';
        $type = ['object' => 'get_class', 'array' => 'array'];
        foreach ($backtrace as $k => $v) {
            foreach ($v['args'] as $ks => $vs) {
                if ($ks === (count($v['args']) - 1)) {
                    $args .= gettype($vs) . '  "' . (!empty($type[gettype($vs)]) ? is_object($vs) ? $type[gettype($vs)]($vs) : $type[gettype($vs)] : (mb_strlen($vs) > 100 ? str_replace(["\r\n", "\n", "\r", "\t"], '\n', mb_substr($vs, 0, 100, 'UTF-8')) : $vs)) . '"';
                } else {
                    $args .= gettype($vs) . '  "' . (!empty($type[gettype($vs)]) ? is_object($vs) ? $type[gettype($vs)]($vs) : $type[gettype($vs)] : (mb_strlen($vs) > 100 ? str_replace(["\r\n", "\n", "\r", "\t"], '\n', mb_substr($vs, 0, 100, 'UTF-8')) : $vs)) . '", ';
                }
            }
            $pre_log_all_content .= "｜    path ({$k}) :  [  " . $v['file'] . "  :  " . $v['line'] . "  ->  " . $v['function'] . " ( {$args} ) ]\n";
            $args = '';
        }
        $pre_log_all_content .= "\r\n[ Run Memory ]    \r\n\r\n";
        $start_memory = isset($_SESSION['pre_start_memory']) ? $_SESSION['pre_start_memory'] : memory_get_usage();
        $pre_log_all_content .= "｜    PHP used memory :   [ " . number_format((memory_get_usage() - $start_memory) / 1024, 2) . " KB ]" . " -- [ " . number_format((memory_get_usage() - $start_memory) / 1024 / 1024, 2) . " MB ]" . "\r\n\r\n";
        $pre_log_all_content .= "｜    Total memory used : [ " . number_format(memory_get_usage() / 1024, 2) . " KB ]" . " -- [ " . number_format(memory_get_usage() / 1024 / 1024, 2) . " MB ]" . "\r\n\r\n";
        $pre_log_all_content .= "｜    System Allocates Maximum Memory : [ " . number_format(memory_get_peak_usage() / 1024, 2) . " KB ]" . " -- [ " . number_format(memory_get_peak_usage() / 1024 / 1024, 2) . " MB ]" . "\r\n";
        $pre_log_all_content .= "\r\n[ Run Time ]    \r\n\r\n";
        $start_time = isset($_SESSION['pre_start_time']) ? $_SESSION['pre_start_time'] : microtime(true);
        $pre_log_all_content .= "｜    PHP used time : [ " . number_format(microtime(true) - $start_time, 8) . "秒 ]\r\n\r\n";
    }
    //只显示路径时间内存
    if ($content === '__show_all__') {
        print_r($pre_log_all_content . $end_pre, true);
        error_log(print_r($pre_log_all_content . $end_pre, true));
        return false;
    }
    if ($content === '__show_all_not_end__') {
        print_r($pre_log_all_content . $end_pre, true);
        error_log(print_r($pre_log_all_content . $end_pre, true));
    }
    if ($content === 'trues') {
        $status = 11;
    }
    if (in_array($status, ['true'])) {
        $status = 11;
    } else if (in_array($status, ['trues'])) {
        $status = 1;
    }
    if ($status === false && is_array($path) && ($path[0] > 0 && $pre_code_wlq === $path[0])) {
        $status = 11;
    }
    if ($pre_code_wlq_stop[0] > 0 && $pre_code_wlq === $pre_code_wlq_stop[0]) {
        $status = 11;
    }
    $pre_log_all_content .= "\r\n[ Print Content ]    \r\n\r\n";
    if (is_object($content)) $pre_log_all_content .= "class." . get_class($content) . ".php\r\n\r\n";
    //打印头部
    print_r($pre_log_all_content);
    //输出正文
    if (in_array($status, [1, 11])) {
        print_r($content);
    } else {
        var_dump($content);
    }
    //打印尾部
    print_r($end_pre);
    //log
    if ($__pre_open_log__ === true) {
        error_log(print_r($pre_log_all_content, true));
        error_log(print_r($content, true));
        error_log(print_r($end_pre, true));
    }
    if ($__pre_open_email_log__ === true) {
        if ($_SESSION['__pre_open_email_log__email_address']['content']) {
            $__pre_open_email_log__email_address = $_SESSION['__pre_open_email_log__email_address']['content'];
        } else {
            $__pre_open_email_log__email_address = print_r($pre_log_all_content, true) . print_r($content, true) . print_r($end_pre, true);
        }
        //sendEmail($_SESSION['__pre_open_email_log__email_address']['receiveEmail'], $__pre_open_email_log__email_address);
    }
    if (in_array($status, [11, 22])) {
        $_SESSION['__pre_code_wlq__'] = 0;
        exit;
    }
    return false;
}

function test_log($log = 'test')
{
    //存储LOG
    pre($log);
}

//发送邮件
/**
 * [email_code 发送邮箱]
 * @param  [type] $receiveEmail [接收的邮箱]
 * @param  [type] $template     [模版ID]
 * @param  array  $params       [根据顺序填写]
 * @return [type]               [description]
 */
function sendEmail($receiveEmail = 'wuliqiang_aa@163.com', $content = '代码测试', $title = '代码测试')
{
    $smtpserver = "smtp.iinu.com.cn"; //SMTP服务器
    $smtpserverport = 25; //SMTP服务器端口
    $smtpusermail = "sendemail@iinu.com.cn"; //SMTP服务器的用户邮箱
    $smtpemailto = $receiveEmail; //发送给谁
    $smtpuser = "sendemail@iinu.com.cn"; //SMTP服务器的用户帐号，注：部分邮箱只需@前面的用户名
    $smtppass = "qwertyuiop_517"; //SMTP服务器的授权码
    $mailtitle = $title; //邮件主题
    $mailcontent = $content; //邮件内容
    $mailtype = "HTML"; //邮件格式（HTML/TXT）,TXT为文本邮件
    //************************ 配置信息 ****************************
    $smtp = new Smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass); //这里面的一个true是表示使用身份验证,否则不使用身份验证.
    $smtp->debug = false; //是否显示发送的调试信息
    $state = $smtp->sendmail($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype);

    if (!$state) {
        echo ('发送失败');exit;
    }
    echo('发送成功');
}
